//
//  HLDoubleSlideView.m
//  ZhiBaoAudio
//
//  Created by zhibao on 2023/8/2.
//

#import "HLDoubleSlideView.h"
#import "NoticePointView.h"
@interface HLDoubleSlideView ()<UIGestureRecognizerDelegate>{
    NSMutableArray *pointArray;
    NoticePointView *drawView;
    NoticePointView *drawView1;
    BOOL moveEndFlag;
}

@property(nonatomic,strong)UIView *leftZDView;
@property(nonatomic,strong)UIView *rightZDView;

@property(nonatomic,assign)CGFloat leftBtnOrgx;
@property(nonatomic,assign)CGFloat rightBtnOrgx;

@end

@implementation HLDoubleSlideView

-(id)init
{
    if (self = [super init]) {
        self.backgroundColor = MyColor(@"#FFF4F4");
        [self setupUI];
        moveEndFlag = NO;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = MyColorAlpha(@"000000", 0.3);
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
 
    pointArray = [NSMutableArray new];
    drawView = [[NoticePointView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height / 2)];
    drawView.bottom = self.height / 2;
    drawView.backgroundColor = UIColor.clearColor;
    drawView.colorStr = @"#ffffff";
    [self addSubview:drawView];
    drawView1 = [[NoticePointView alloc]initWithFrame:CGRectMake(0, drawView.bottom, drawView.width, drawView.height)];
    drawView1.backgroundColor = UIColor.clearColor;
    drawView1.colorStr = @"#ffffff";
    drawView1.transform = CGAffineTransformMakeScale(1.0, -1.0);
    [self addSubview:drawView1];
    pointArray = [NSMutableArray new];
    for(int i = 0; i < 300; i ++ ){
        srand((unsigned)time(0));
        int num = arc4random() % (int)drawView.height;
        //DLog(@"%d",num);
        CGPoint point = CGPointMake(drawView.height,num);
        [pointArray insertObject:[NSValue valueWithCGPoint:point] atIndex:0];
    }
    drawView.pointArray = pointArray;
    drawView1.pointArray = pointArray;
    
    _leftZDView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, self.height)];
    _leftZDView.backgroundColor = MyColorAlpha(@"#333333", 0.7);
    [self addSubview:_leftZDView];
    
    _rightZDView = [[UIView alloc]initWithFrame:CGRectMake(self.width, 0, 0, self.height)];
    _rightZDView.backgroundColor = MyColorAlpha(@"#333333", 0.7);
    [self addSubview:_rightZDView];
    
    _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBtn.frame = CGRectMake(-25, 50, 50,50);
    _leftBtn.centerY = self.height / 2;
    [_leftBtn setImage:[UIImage imageNamed:@"h_cj_left"] forState:UIControlStateNormal];
    [self addSubview:_leftBtn];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    panGesture.delegate = self;
    [_leftBtn addGestureRecognizer:panGesture];
    
    _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _leftBtn.bottom, 60, 30)];
    _leftLabel.backgroundColor = [UIColor clearColor];
    _leftLabel.font = MY_SCALE_RegularFONT(10);
    _leftLabel.textAlignment = NSTextAlignmentCenter;
    _leftLabel.textColor = MyColor(@"ffffff");
    [self addSubview:_leftLabel];
    
    _leftLabel.centerX = _leftBtn.centerX;
    
    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn.backgroundColor = [UIColor clearColor];
    _rightBtn.frame = CGRectMake(self.width - 25, 50, 40, 50);
    _rightBtn.centerY = self.height / 2;
    [_rightBtn setImage:[UIImage imageNamed:@"h_cj_right"] forState:UIControlStateNormal];
    panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
    panGesture.delegate = self;
    [_rightBtn addGestureRecognizer:panGesture];
    
    _rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _rightBtn.bottom, 60, 30)];
    _rightLabel.backgroundColor = [UIColor clearColor];
    _rightLabel.font = MY_SCALE_RegularFONT(10);
    _rightLabel.textAlignment = NSTextAlignmentCenter;
    _rightLabel.textColor = MyColor(@"ffffff");
    [self addSubview:_rightLabel];
    
    _rightLabel.centerX = _rightBtn.centerX;
    [self addSubview:_rightBtn];
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@"doubleView hitTest");
    return [super hitTest:point withEvent:event];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"began");
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"move");
    [super touchesMoved:touches withEvent:event];
}


-(void)layoutSubviews
{
    
    CGFloat leftCentenX = (_currentLeftValue - _minValue) * self.bounds.size.width/(_maxValue - _minValue) ;
    _leftBtn.centerX = leftCentenX;
    
//    if (_currentLeftValue != 0) {
//        CGFloat centenX = (_currentRightValue - _minValue) * (self.bounds.size.width) / (_maxValue - _minValue) ;
//        _rightBtn.centerX = centenX;
//    }else{
//        _rightBtn.centerX = self.bounds.size.width;
//    }
    
    CGFloat rightCentenX = (_currentRightValue - _minValue) * (self.bounds.size.width) / (_maxValue - _minValue) ;
    _rightBtn.centerX = rightCentenX;
    
    _leftLabel.centerX = _leftBtn.centerX;
    _rightLabel.centerX = _rightBtn.centerX;
    _leftZDView.width = _leftBtn.centerX;
    _rightZDView.left = _rightBtn.centerX;
    _rightZDView.width = self.width - _rightBtn.centerX;
    if (_block) {
        _leftLabel.text = _block(_currentLeftValue);
        _rightLabel.text = _block(_currentRightValue);
    }
}

-(void)tapGestureAction:(UIPanGestureRecognizer*)panGesture
{
    UIView *vw = panGesture.view;
    
    CGPoint transPoint = [panGesture translationInView:self];
    //NSLog(@"x:%lf,y:%lf",transPoint.x,transPoint.y);
    
    switch (panGesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            if ([vw isEqual:_leftBtn])
            {
                _leftBtnOrgx = _leftBtn.centerX;
                NSLog(@"拖拽左边按钮");
                
            }
            else if([vw isEqual:_rightBtn])
            {
                _rightBtnOrgx = _rightBtn.centerX;
                NSLog(@"拖拽右边按钮");
            }
            
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            
            if ([vw isEqual:_leftBtn])
            {
                
                CGFloat orginX = _leftBtn.centerX;
                _leftBtn.centerX = _leftBtnOrgx + transPoint.x;
                if (_leftBtn.centerX < 0) {
                    _leftBtn.centerX = 0;
                }
                else if(_leftBtn.centerX >= _rightBtn.centerX - 25)
                {
                    _leftBtn.centerX = orginX;
                }
                _leftLabel.centerX = _leftBtn.centerX;
                _leftZDView.width = _leftBtn.centerX;
            }
            else if([vw isEqual:_rightBtn])
            {
                CGFloat orginX = _rightBtn.centerX;
                _rightBtn.centerX = _rightBtnOrgx + transPoint.x;
                if (_rightBtn.centerX >= self.bounds.size.width) {
                    _rightBtn.centerX = self.bounds.size.width;
                }
                else if(_rightBtn.centerX <= _leftBtn.centerX + 25)
                {
                    _rightBtn.centerX = orginX;
                }
                _rightLabel.centerX = _rightBtn.centerX;
                _rightZDView.left = _rightBtn.centerX;
                _rightZDView.width = self.width - _rightBtn.centerX;
            }
            
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            moveEndFlag = YES;
        }
            break;
            
        default:
            break;
    }
    _currentLeftValue = _minValue + (_maxValue - _minValue) * ((_leftBtn.centerX) / (self.bounds.size.width));
    _currentRightValue = _minValue + (_maxValue - _minValue) * ((_rightBtn.centerX) / (self.bounds.size.width));
    if (_block) {
        _leftLabel.text = _block(_currentLeftValue);
        _rightLabel.text = _block(_currentRightValue);
    }
    if(moveEndFlag){
        moveEndFlag = NO;
        if (_valueBlock) {
            NSLog(@"leftValue:%lf,rightValue:%lf",_currentLeftValue,_currentRightValue);
            self.valueBlock(_currentLeftValue, _currentRightValue);
        }
    }
    [self setNeedsDisplay];
}


- (void)setRightLabelTexgt:(NSString *)text{
    _rightLabel.text = text;
}

-(void)setCurrentLeftValue:(CGFloat)currentLeftValue
{
    _currentLeftValue = currentLeftValue;
    CGFloat centenX = 0;
    if(_maxValue - _minValue > 0){
        centenX = (currentLeftValue - _minValue) * (self.bounds.size.width)/(_maxValue - _minValue);
    }
    _leftBtn.centerX = centenX;
    [self setNeedsDisplay];
}

-(void)setCurrentRightValue:(CGFloat)currentRightValue
{
    _currentRightValue = currentRightValue;
    CGFloat centenX = 0;
    if(_maxValue - _minValue > 0){
        centenX = (_currentRightValue - _minValue) * (self.bounds.size.width) / (_maxValue - _minValue);
    }
    _rightBtn.centerX = centenX;
    _rightZDView.left = _rightBtn.centerX;
    [self setNeedsDisplay];
}

- (void)setMaxValue:(CGFloat)maxValue{
    _maxValue = maxValue;
}

-(void)drawRect:(CGRect)rect
{
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetLineCap(context, kCGLineCapRound);
//    CGContextSetLineWidth(context, 3);
//    [[UIColor grayColor] setStroke];
//    CGContextMoveToPoint(context, 0, self.height / 2 - 1.5);
//    CGContextAddLineToPoint(context, self.bounds.size.width, self.height / 2 - 1.5);
//    CGContextStrokePath(context);
//
//    [[UIColor redColor] setStroke];
//    CGContextMoveToPoint(context, _leftBtn.centerX, self.height / 2 - 1.5);
//    CGContextAddLineToPoint(context, _rightBtn.centerX,self.height / 2 - 1.5);
//    CGContextStrokePath(context);
    
}

@end
